package com.fbd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbd.common.CustomException;
import com.fbd.entity.Category;
import com.fbd.entity.Dish;
import com.fbd.entity.Setmeal;
import com.fbd.service.CategoryService;
import com.fbd.service.DishService;
import com.fbd.service.SetmealService;
import com.fbd.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    public DishService dishService;
    @Autowired
    public SetmealService setmealService;
    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);

        //1、查询当前分类是否关联了其它菜品，若关联则抛出异常
        if(count1 > 0){
            throw new CustomException("当前分类项关联了菜品，不能删除");
        }
        //2、查询当前分类是否关联了其它套餐，若关联则抛出异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("当前分类项关联了套餐，不能删除");
        }

        //正常删除
        super.removeById(id);
    }
}
